﻿using UnityEngine;
using System.Collections;

[System.Serializable]
public class StatBody
{
	public StatData head = new StatData();
	public StatData lArm = new StatData();
	public StatData rArm = new StatData();
	public StatData torso = new StatData();
	public StatData lLeg = new StatData();
	public StatData rLeg = new StatData();

	// Used by damage algorithme
	public static BodyType[][] headLinks = new BodyType[3][] {	new BodyType[] {BodyType.L_Arm, BodyType.R_Arm},
																new BodyType[] {BodyType.Torso},
																new BodyType[] {BodyType.L_Leg, BodyType.R_Leg}};
	public static BodyType[][] lArmLinks = new BodyType[3][] {	new BodyType[] {BodyType.Head, BodyType.Torso},
																new BodyType[] {BodyType.R_Arm},
																new BodyType[] {BodyType.R_Arm, BodyType.R_Leg}};
	public static BodyType[][] rArmLinks = new BodyType[3][] {	new BodyType[] {BodyType.Head, BodyType.Torso},
																new BodyType[] {BodyType.L_Arm},
																new BodyType[] {BodyType.L_Leg, BodyType.R_Leg}};
	public static BodyType[][] torsoLinks = new BodyType[1][] {	new BodyType[] {BodyType.L_Arm, BodyType.R_Arm, BodyType.Head, BodyType.L_Leg, BodyType.R_Leg}};
	public static BodyType[][] lLegLinks = new BodyType[3][] {	new BodyType[] {BodyType.Torso, BodyType.R_Leg},
																new BodyType[] {BodyType.L_Arm, BodyType.R_Arm},
																new BodyType[] {BodyType.Head}};
	public static BodyType[][] rLegLinks = new BodyType[3][] {	new BodyType[] {BodyType.Torso, BodyType.L_Leg},
																new BodyType[] {BodyType.L_Arm, BodyType.R_Arm},
																new BodyType[] {BodyType.Head}};

	public void ApplyDamage(StatBody _body)
	{

	}

	public void ApplyDamage(float[] _damages)
	{
		head.minValue = Mathf.Clamp(head.minValue - _damages[0], 0f, 1000f);
		lArm.minValue = Mathf.Clamp(lArm.minValue - _damages[1], 0f, 1000f);
		rArm.minValue = Mathf.Clamp(rArm.minValue - _damages[2], 0f, 1000f);
		torso.minValue = Mathf.Clamp(torso.minValue - _damages[3], 0f, 1000f);
		lLeg.minValue = Mathf.Clamp(lLeg.minValue - _damages[4], 0f, 1000f);
		rLeg.minValue = Mathf.Clamp(rLeg.minValue - _damages[5], 0f, 1000f);
	}
	
	public StatData GetStat(BodyType _type)
	{
		switch (_type)
		{
		case BodyType.Head:		return head;
		case BodyType.L_Arm:	return lArm;
		case BodyType.R_Arm:	return rArm;
		case BodyType.Torso:	return torso;
		case BodyType.R_Leg:	return lLeg;
		case BodyType.L_Leg:	return rLeg;
		}
		
		return null;
	}
	
	public void SetStat(BodyType _type, StatData _stat)
	{
		switch (_type)
		{
		case BodyType.Head:		head = _stat;		break;
		case BodyType.L_Arm:	lArm = _stat;		break;
		case BodyType.R_Arm:	rArm = _stat;		break;
		case BodyType.Torso:	torso = _stat;		break;
		case BodyType.R_Leg:	lLeg = _stat;		break;
		case BodyType.L_Leg:	rLeg = _stat;		break;
		}
	}
	
	public static float[] CalculateDamage(BodyType _aimed, float _accuracy, float _initialDamage)
	{
		BodyType[][] links = new BodyType[3][];
		switch (_aimed)
		{
		case BodyType.Head:		links = headLinks;	break;
		case BodyType.L_Arm:	links = lArmLinks;	break;
		case BodyType.R_Arm:	links = rArmLinks;	break;
		case BodyType.Torso:	links = torsoLinks;	break;
		case BodyType.L_Leg:	links = lLegLinks;	break;
		case BodyType.R_Leg:	links = rLegLinks;	break;
		}

		_accuracy /= 100f;
		float[] damage = new float[6];
		damage[(int)_aimed] = _accuracy * _initialDamage;
		float currentDamage = _initialDamage - (_accuracy * _initialDamage);

		for (int i = 0; i < links.Length; i++)
		{
			float value = currentDamage * _accuracy;
			foreach (BodyType type in links[i])
			{
				damage[(int)type] = value / links[i].Length;
			}

			currentDamage -= value;
		}

		for (int i = 0; i < damage.Length; i++)
		{
			damage[i] += currentDamage / damage.Length;
		}

		return damage;
	}

	public static BodyType RandomBodyType()
	{
		string[] items = System.Enum.GetNames(typeof(BodyType));
		return (BodyType)System.Enum.Parse(typeof(BodyType), items[Random.Range(0, items.Length)]);
	}
}
